var walkdir = 0;
var pixmov = 8;
var pixmoved = 0;

var cx, cy;
var ang;
var acc = 1;
var BEINGKILLED = false;
var RECOUCH = false;
var font = GetSystemFont();
var couch1=0, couch2=0, couch3=0, couch4=0, couch5=0, couch6=0;
var cos=Math.cos, sin=Math.sin, invt=Math.atan2, sqrt=Math.sqrt, pow=Math.pow, abs=Math.abs, PI=Math.PI, floor=Math.floor;


var wx, wy, vx, vy;
var wstairdir=0, vstairdir=0;
var wedge = CreatePerson('wedge', 'Brown.rss', false);
var vicks = CreatePerson('vicks', 'Brown.rss', false);
SetRenderScript("bastard_init();");
MapEngine("Albrook_entless_newer.rmp",45);

function bastard_init()
{
	SetPersonX('wedge', 68*16+7); SetPersonY('wedge', 22*16+7);
	SetPersonX('vicks', 72*16+5); SetPersonY('vicks', 22*16+7);
	IgnorePersonObstructions('wedge', true);
	IgnorePersonObstructions('vicks', true);
	SetPersonDirection('wedge', 'heast');
	SetPersonDirection('vicks', 'hwest');
	couch1=GetTileImage(1905);
	couch2=GetTileImage(1906);
	couch3=GetTileImage(1907);
	couch4=GetTileImage(1908);
	couch5=GetTileImage(1909);
	couch6=GetTileImage(1910);
	SetLayerVisible(4,true);
	SetUpdateScript("bastard();");
	SetRenderScript("");
	SetLayerRenderer(1,"shutup();");
}


function bastard()
{
	wx = GetPersonXFloat('wedge'), wy = GetPersonYFloat('wedge');
	vx = GetPersonXFloat('vicks'), vy = GetPersonYFloat('vicks');
	
		// checking for stairs
	if (abs(wx - 1048) < 39 && abs(wy - 458) < 57)		//1st set
		wstairdir = 1;
	else if (abs(wx - 928) < 46 && abs(wy - 582) < 57)	//2nd set
		wstairdir = 2;
	else wstairdir = 0;
	
	if (abs(vx - 1048) < 39 && abs(vy - 458) < 57)
		vstairdir = 1;
	else if (abs(vx - 928) < 46 && abs(vy - 582) < 57)
		vstairdir = 2;
	else vstairdir = 0;
	
	if (IsKeyPressed(KEY_4)) {SetPersonXYFloat('wedge', 68*16+7, 22*16+7);
						SetPersonXYFloat('vicks', 72*16+5, 22*16+7);
						BEINGKILLED = false;}
	
	if (!BEINGKILLED && !RECOUCH)
	{
	if (wstairdir==1 || vstairdir==1)
		{ IgnoreTileObstructions('wedge', true); IgnoreTileObstructions('vicks', true); 
		  SetPersonDirection('wedge', 'hnortheast'); SetPersonDirection('vicks', 'hsouthwest');}
	else if (wstairdir==2 || vstairdir==2)
		{ IgnoreTileObstructions('wedge', true); IgnoreTileObstructions('vicks', true); 
		  SetPersonDirection('wedge', 'hsoutheast'); SetPersonDirection('vicks', 'hnorthwest');}
	else  { IgnoreTileObstructions('wedge', false); IgnoreTileObstructions('vicks', false); 
		  SetPersonDirection('wedge', 'heast'); SetPersonDirection('vicks', 'hwest');}
		
	if (wstairdir && !vstairdir) var wpointeight = true;
	if (vstairdir && !wstairdir) var vpointeight = true;

	if (IsKeyPressed(KEY_A)) acc +=.5;
	if (!wstairdir && !vstairdir){ vy = wy; wx = vx-(4*16-2);}
	if (!walkdir)
	{
		if (IsKeyPressed(KEY_UP) && !wstairdir && !vstairdir)
			walkdir = COMMAND_MOVE_NORTH;
		else if (IsKeyPressed(KEY_DOWN) && !wstairdir && !vstairdir)
			walkdir = COMMAND_MOVE_SOUTH;
		else if (IsKeyPressed(KEY_RIGHT))
			walkdir = COMMAND_MOVE_EAST;
		else if (IsKeyPressed(KEY_LEFT))
			walkdir = COMMAND_MOVE_WEST;
	}
	if (walkdir && pixmov)
	{
		if (walkdir==9)
		{
			if (IsPersonObstructed('wedge', wx,	 wy-1) || IsPersonObstructed('wedge', wx+16, wy-1)
			|| IsPersonObstructed('wedge', wx+32, wy-1) || IsPersonObstructed('wedge', wx+48, wy-1)
			|| IsPersonObstructed('wedge', wx+59, wy-1))
				{ walkdir = 0; pixmov = 8; }
			else
			{
				SetPersonXYFloat('wedge', wx, wy-1);
				SetPersonXYFloat('vicks', vx, vy-1);
				if (pixmov % 8==0){frameup('wedge');frameup('vicks');}	//animate
				pixmov--;
			}
		}
		else if (walkdir==11)
		{
			if (IsPersonObstructed('wedge', wx,      wy+1) || IsPersonObstructed('wedge', wx+16, wy+1)
			|| IsPersonObstructed('wedge', wx+32, wy+1) || IsPersonObstructed('wedge', wx+48, wy+1)
			|| IsPersonObstructed('wedge', wx+59, wy+1))
				{ walkdir = 0; pixmov = 8; }
			else
			{
				SetPersonXYFloat('wedge', wx, wy+1);
				SetPersonXYFloat('vicks', vx, vy+1);
				if (pixmov % 8==0){frameup('wedge');frameup('vicks');}
				pixmov--;
			}
		}
		else if (walkdir==12)
		{
			acceljunkL();
			if (IsPersonObstructed('wedge', wx-acc, wy)
			|| IsPersonObstructed('vicks', vx-acc, vy))
				{ walkdir = 0; pixmov = 8; }
			else
			{								//pos + acc * stairs? * up_or_down * 0.8?
				SetPersonXYFloat('wedge', wx-acc*((wpointeight)?.8:1), wy+acc*((wstairdir)?1:0)*((wstairdir==1)?1:-1)*((wpointeight)?(0.8):1) );
				SetPersonXYFloat('vicks', vx-acc*((vpointeight)?.8:1), vy+acc*((vpointeight)?(0.8):1)*((vstairdir==1)?1:-1)*((vstairdir)?1:0) );
				if (pixmov % (8 - 4*((wstairdir) ? 1: 0)) == 0) frameup('wedge');
				if (pixmov % (8 - 4*((vstairdir) ? 1 : 0)) == 0) frameup('vicks');
				pixmov--;
			}
		}
		else if (walkdir==10)
		{
			acceljunkR();
			if (IsPersonObstructed('wedge', wx+acc, wy)
			|| IsPersonObstructed('vicks', vx+acc, vy))
				{ walkdir = 0; pixmov = 8; }
			else
			{
				SetPersonXYFloat('wedge', wx+acc*((wpointeight)?.8:1), wy+acc*((wstairdir)?1:0)*((wstairdir==1)?-1:1)*((wpointeight)?(0.8):1) );
				SetPersonXYFloat('vicks', vx+acc*((vpointeight)?.8:1), vy+acc*((vpointeight)?(0.8):1)*((vstairdir==1)?-1:1)*((vstairdir)?1:0) );
				if (pixmov % (8 - 4*((wstairdir) ? 1: 0)) == 0) frameup('wedge');
				if (pixmov % (8 - 4*((vstairdir) ? 1 : 0)) == 0) frameup('vicks');
				pixmov--;
			}
		}
	}
	if (!pixmov)
	{
		walkdir = 0;
		pixmov = 8;
	}
	wx = GetPersonXFloat('wedge'), wy = GetPersonYFloat('wedge');
	vx = GetPersonXFloat('vicks'), vy = GetPersonYFloat('vicks');
		if (!BEINGKILLED)
		{
			cx = (wstairdir==2 || vstairdir==2) ? wx+6 : vx-7;
			cy = (wstairdir==2 || vstairdir==2) ? wy : vy;
			ang = -invt(vy-wy, vx-wx)*(180/PI);
		}
	}
	else
	{
		if (BEINGKILLED==1)
			killprocess1();
		else
			killprocess2();
	}
	SetCameraX(wx + (abs(wx-vx)/2));
	SetCameraY(((wstairdir==2 || vstairdir==2) ? wy : vy) + (abs(wy-vy)/2));
}

function shutup()
{
	var sx = map2screenX(cx);
	var sy = map2screenY(cy);

	// bottom-right rotation (stairmode 0 & 1) //
	if (wstairdir==1 || vstairdir==1 || (wstairdir==0 && vstairdir==0))
	{
	couch1.rotateBlit(sx+46.6*cos(rad(ang+180-31.0))-8,	sy-46.6*sin(rad(ang+180-31.0))-8,	rad(-ang));
	couch4.rotateBlit(sx+40.8*cos(rad(ang+180-11.3))-8,	sy-40.8*sin(rad(ang+180-11.3))-8,	rad(-ang));
	
	couch2.rotateBlit(sx+33.9*cos(rad(ang+180-45))-8,	sy-33.9*sin(rad(ang+180-45))-8,	rad(-ang));
	couch5.rotateBlit(sx+25.3*cos(rad(ang+180-18.4))-8,	sy-25.3*sin(rad(ang+180-18.4))-8,	rad(-ang));
	
	couch3.rotateBlit(sx+25.3*cos(rad(ang+180-71.6))-8,	sy-25.3*sin(rad(ang+180-71.6))-8,	rad(-ang));
	couch6.rotateBlit(sx+11.3*cos(rad(ang+180-45))-8,	sy-11.3*sin(rad(ang+180-45))-8,	rad(-ang));
	}
	
	// bottom-left rotation (stairmode 2) //
	else if (wstairdir==2 || vstairdir==2)
	{
	couch1.rotateBlit(sx+25.3*cos(rad(ang+71.6))-8,	sy-25.3*sin(rad(ang+71.6))-8,	rad(-ang));
	couch4.rotateBlit(sx+11.3*cos(rad(ang+45))-8,	sy-11.3*sin(rad(ang+45))-8,	rad(-ang));
		
	couch2.rotateBlit(sx+33.9*cos(rad(ang+45))-8,	sy-33.9*sin(rad(ang+45))-8,	rad(-ang));
	couch5.rotateBlit(sx+25.3*cos(rad(ang+18.4))-8,	sy-25.3*sin(rad(ang+18.4))-8,	rad(-ang));
	
	couch3.rotateBlit(sx+46.6*cos(rad(ang+31.0))-8,	sy-46.6*sin(rad(ang+31.0))-8,	rad(-ang));
	couch6.rotateBlit(sx+40.8*cos(rad(ang+11.3))-8,	sy-40.8*sin(rad(ang+11.3))-8,	rad(-ang));
	}
}
	

function map2screenX(X)
{
	if (GetScreenWidth()/2 < GetCameraX())
		if (GetCameraX() > GetLayerWidth(0)*16 - GetScreenWidth()/2)
			return GetScreenWidth() - (GetLayerWidth(0)*16 - X);
		else return GetScreenWidth()/2 + (X - GetCameraX());
	else return X; 
}

function map2screenY(Y)
{
	if (GetScreenHeight()/2 < GetCameraY())
		if (GetCameraY() > GetLayerHeight(0)*16 - GetScreenHeight()/2)
			return GetScreenHeight() - (GetLayerHeight(0)*16 - Y);
		else return GetScreenHeight()/2 + (Y - GetCameraY());
	else return Y;
}


function rad(ical)
{
	return ical*PI/180;
}

function frameup(sir)	//hauling dir
{
	if (GetPersonFrame(sir) == 3)
		SetPersonFrame(sir, 0);
	else SetPersonFrame(sir, GetPersonFrame(sir)+1);
}

function framedown(ghe)	//rolling dir
{
	if (GetPersonFrame(ghe) == 0)
		SetPersonFrame(ghe, 4);
	else SetPersonFrame(ghe, GetPersonFrame(ghe)-1);
}


function killprocess1()
{
	if (pixmov)
	{
		if (wstairdir==1 || vstairdir==1)
		{
			SetPersonDirection('wedge', 'rolling');
			if (pixmov % 4 == 0) framedown('wedge');
			
			if (pixmov > 60) SetPersonXYFloat('wedge', wx-1, wy+1);
			else			SetPersonXYFloat('wedge', wx-1, wy);
			
			if (pixmov > 40 && pixmoved > 3)		//COUCH SLIDING
				if (pixmoved%4==0)	{ang=40;cy+=4;}
				else				{ang=35;cx-=1;}
			
			if (pixmoved == 3)
				{SetPersonDirection('vicks', 'crouch'); SetPersonXYFloat('vicks', vx-2, vy+2); }
			else if (pixmoved == 6)
				{SetPersonXYFloat('vicks', vx-2, vy+2); }
			else if (pixmoved == 9)
				SetPersonXYFloat('vicks', vx-2, vy+2);
			else if (pixmoved == 12)
				{SetPersonDirection('vicks', 'down1');SetPersonXYFloat('vicks', vx-2, vy+2);}

			pixmov--;
			pixmoved++;
		}
		if (pixmov < 30) pixmov = 0;	//dont roll so far
	}
	else
	{
		if (!RECOUCH)
		{
			SetPersonDirection('wedge', 'down1');
			acc=1;
			pixmoved = 0;
			recouch1();
			RECOUCH = true;
		}
	}
}


function recouch1()
{
	//WEDGE
	for (var f=0; f<51; f++)
	QueuePersonCommand('wedge', 0, 0);	//wait
	
	QueuePersonScript('wedge', 'SetPersonDirection("wedge", "crouch");', 0);	//get up
	for (var f=0; f<26; f++)
	QueuePersonCommand('wedge', 0, 0);	//wait some more
	
	QueuePersonCommand('wedge', 3, 0);	//face east
	for (var f=0; f<(cx - GetPersonX('wedge')-54); f++)
	QueuePersonCommand('wedge', 10, 0);	//walk back

	//VICKS
	for (var f=0; f<46; f++)
	QueuePersonCommand('vicks', 0, 0);	//wait
	
	QueuePersonScript('vicks', 'SetPersonDirection("vicks", "crouch");', 0);	//get up
	for (var f=0; f<26; f++)
	QueuePersonCommand('vicks', 0, 0);	//wait some more
	
	QueuePersonCommand('vicks', 7, 0);	//face west
	for (var f=0; f<(GetPersonX('vicks') - cx); f++)
	{
	QueuePersonCommand('vicks', 11, 0);	//walk south
	QueuePersonCommand('vicks', 12, 1);	//walk west
	}
	QueuePersonScript('vicks', 'RECOUCH=false; BEINGKILLED=false;', 0);
}

function acceljunkL()
{
	if (IsKeyPressed(KEY_LEFT))
	{
		if (wstairdir==1 || vstairdir==1)
			{if (acc<4) acc+=.14;}
		else{if (acc!=1) acc-=.14; if (acc<1) acc=1;}
	}
	else {if (acc!=1) acc-=.14; if (acc<1) acc=1;}
	if (IsKeyPressed(KEY_RIGHT))
		if (acc!=1){acc-=.14; if (acc<1) acc=1;}			//pull other way to stop
	if (pixmov<20) pixmov += floor(acc-.0625);			//build up momentum
	if (vstairdir==1 && acc>=4)						//too fast: die
	{
		if (wx - 59.5*16+7 > 26)						//not too close to bottom
		{
			BEINGKILLED = 1;
			pixmov = floor(wx - 59.5*16+7);
		}
	}
}

function acceljunkR()
{
	if (IsKeyPressed(KEY_RIGHT))
	{
		if (wstairdir==2 || vstairdir==2)
			{if (acc<4) acc+=.14;}
		else{if (acc!=1) acc-=.14; if (acc<1) acc=1;}
	}
	else {if (acc!=1) acc-=.14; if (acc<1) acc=1;}
	if (IsKeyPressed(KEY_LEFT))
		if (acc!=1){acc-=.14; if (acc<1) acc=1;}			//pull other way to stop
	if (pixmov<20) pixmov += floor(acc-.0625);			//build up momentum
	if (wstairdir==2 && acc>=4)						//too fast: die
	{
		if (63.5*16+7 - vx > 26)						//not too close to bottom
		{
			BEINGKILLED = 2;
			pixmov = floor(63.5*16+7 - vx);
		}
	}
}


function killprocess2()
{
	if (pixmov)
	{
		if (wstairdir==2 || vstairdir==2)
		{
			SetPersonDirection('vicks', 'rolling');
			if (pixmov % 4 == 0) framedown('vicks');
			
			if (pixmov > 60) SetPersonXYFloat('vicks', vx+1, vy+1);
			else			SetPersonXYFloat('vicks', vx+1, vy);
			
			if (pixmov > 40 && pixmoved > 3)		//COUCH SLIDING
				if (pixmoved%4==0)	{ang=-40;cy+=4;}
				else				{ang=-35;cx+=1;}
			
			if (pixmoved == 3)
				{SetPersonDirection('wedge', 'crouch'); SetPersonXYFloat('wedge', wx+2, wy+2); }
			else if (pixmoved == 6)
				{SetPersonXYFloat('wedge', wx+2, wy+2); }
			else if (pixmoved == 9)
				SetPersonXYFloat('wedge', wx+2, wy+2);
			else if (pixmoved == 12)
				{SetPersonDirection('wedge', 'down1');SetPersonXYFloat('wedge', wx+2, wy+2);}

			pixmov--;
			pixmoved++;
		}
		if (pixmov < 30) pixmov = 0;	//dont roll so far
	}
	else
	{
		if (!RECOUCH)
		{
			SetPersonDirection('vicks', 'down1');
			acc=1;
			pixmoved = 0;
			recouch2();
			RECOUCH = true;
		}
	}
}


function recouch2()
{
	//VICKS
	for (var f=0; f<51; f++)
	QueuePersonCommand('vicks', 0, 0);	//wait
	
	QueuePersonScript('vicks', 'SetPersonDirection("vicks", "crouch");', 0);	//get up
	for (var f=0; f<26; f++)
	QueuePersonCommand('vicks', 0, 0);	//wait some more
	
	QueuePersonCommand('vicks', 7, 0);	//face west
	for (var f=0; f<(GetPersonX('vicks') - (cx+54)); f++)
	QueuePersonCommand('vicks', 12, 0);	//walk back

	//WEDGE
	for (var f=0; f<46; f++)
	QueuePersonCommand('wedge', 0, 0);	//wait
	
	QueuePersonScript('wedge', 'SetPersonDirection("wedge", "crouch");', 0);	//get up
	for (var f=0; f<26; f++)
	QueuePersonCommand('wedge', 0, 0);	//wait some more
	
	QueuePersonCommand('wedge', 3, 0);	//face east
	for (var f=0; f<(cx - GetPersonX('wedge')); f++)
	{
	QueuePersonCommand('wedge', 11, 0);	//walk south
	QueuePersonCommand('wedge', 10, 1);	//walk east
	}
	QueuePersonScript('wedge', 'RECOUCH=false; BEINGKILLED=false;', 0);
}
